package org.lib.hadoop.hello;

import java.io.IOException;
import java.net.URI;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.RemoteIterator;
import org.apache.hadoop.hdfs.client.HdfsAdmin;
import org.apache.hadoop.hdfs.protocol.CachePoolEntry;
import org.apache.hadoop.hdfs.protocol.EncryptionZone;
import org.apache.hadoop.hdfs.protocol.OpenFileEntry;
import org.apache.hadoop.hdfs.protocol.ZoneReencryptionStatus;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * HdfsAdmin
 * @author ben
 * @date 2021-12-13 20:57:05 CST
 */
//@Component
@Slf4j
public class HdfsAdminRunner implements CommandLineRunner {

	private static String haddr = "hdfs://localhost:19870";
	
	@Override
	public void run(String... args) throws Exception {
		Configuration hconf = new Configuration();
		
		HdfsAdmin ha = new HdfsAdmin(new URI(haddr), hconf);
		
		inspectHdfsAdmin(ha);
	}

	/**
	 * 1、检查HdfsAdmin
	 * @author ben
	 * @date 2021-12-13 20:58:41 CST
	 * @param ha
	 */
	private void inspectHdfsAdmin(HdfsAdmin ha) {
		System.out.println("ha=" + ha);
		try {
			System.out.println("ha.KeyProvider=" + ha.getKeyProvider());
			
			RemoteIterator<CachePoolEntry> iter1 = ha.listCachePools();
			System.out.println("iter1=" + iter1);
			while (iter1.hasNext()) {
				System.out.println(iter1.next());
			}
			
			RemoteIterator<EncryptionZone> iter2 = ha.listEncryptionZones();
			System.out.println("iter2=" + iter2);
			while (iter2.hasNext()) {
				System.out.println(iter2.next());
			}
			
			RemoteIterator<OpenFileEntry> iter3 = ha.listOpenFiles();
			System.out.println("iter3=" + iter3);
			while (iter3.hasNext()) {
				System.out.println(iter3.next());
			}
			
			RemoteIterator<ZoneReencryptionStatus> iter4 = ha.listReencryptionStatus();
			System.out.println("iter4=" + iter4);
			while (iter4.hasNext()) {
				System.out.println(iter4.next());
			}
			
		} catch (IOException e) {
			System.out.println("发生异常：e=" + e);
		}
		
		System.out.println("inspectHdfsAdmin END\n");
	}

}
